package com.kaifamiao.high.thread;

/**
 * @author lucien-ma
 * @date 2021/9/29
 * @apiNote
 */
public class Demo03 {
    public static void main(String[] args) {
        // 创建任务
        TicketRunnable2 ticketRunnable = new TicketRunnable2();
        new Thread(ticketRunnable, "一号窗口").start();
        new Thread(ticketRunnable, "二号窗口").start();
        new Thread(ticketRunnable, "三号窗口").start();
        new Thread(ticketRunnable, "四号窗口").start();
        new Thread(ticketRunnable, "五号窗口").start();
    }

}


class TicketRunnable2 implements Runnable {
    private int num = 100;

    @Override
    public void run() {
        while (true) {
            if (num > 0) {
                sell();
                try {
                    Thread.currentThread().sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                break;
            }
        }
    }

    /**
     * 实例方法锁的是 this，静态方法是 类.class
     */
    private synchronized void sell() {
        if (num > 0) {
            System.out.println(Thread.currentThread().getName()+"卖出第"+ num + "张票");
            num --;
        }
    }
}